package cjlu.ea.h.common;

import io.swagger.annotations.ApiModelProperty;
import io.swagger.models.auth.In;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.HashMap;
import java.util.Map;

@Data

public class R {
    @ApiModelProperty(value ="是否成功")
    private boolean success;

    @ApiModelProperty(value ="返回码")
    private Integer code;//20000 成功,20001 一般 error , 50010 未登录 ,50014 token 过期

    @ApiModelProperty(value ="返回消息")
    private String message;

    @ApiModelProperty(value ="返回数据")
    private Map<String,Object> data = new HashMap<>();

    private R(){}

    public static R ok(){
        R r = new R();
        r.setSuccess(true);
        r.setCode(20000);
        r.setMessage("成功");
        return r;
    }

    public static R error(Integer code){
        R r = new R();
        r.setSuccess(false);
        r.setCode(code);
        r.setMessage("失败");

        return r;
    }
    public static R error(){
        R r = new R();
        r.setSuccess(false);
        r.setCode(20001);
        r.setMessage("失败");

        return r;
    }

    public R data(Map<String,Object> map){
        this.data = map;
        return this;
    }

    public R data(String key,Object value){
        this.data.put(key,value);
        return this;
    }

    public R message(String message){
        this.message = message;
        return this;
    }

}
